package com.cuit.springboot.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 处理所有不可知的异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    ServerResponse<?> handleException(Exception e){
        LOGGER.error(e.getMessage(), e);
        return ServerResponse.createByError("操作失败！");
    }

    /**
     * 处理所有业务异常
     * @param e
     * @return
     */
    @ExceptionHandler(MyException.class)
    @ResponseBody
    ServerResponse<?> handleBusinessException(MyException e){
        LOGGER.error(e.getMessage(), e);
        return  ServerResponse.createByError(e.getMessage());
    }
}
